﻿using GKQC.Common;
using GKQC.DbHelper.Dao;
using GKQC.DbHelper.DbModels;
using GKQC.DbHelper.Services;
using GKQC.Language;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace GKQC
{
    public partial class FormLog : Form
    {
        UserModel user = null;
        AllDbContext dbContext = null;
        List<LogModel> _Logs = null;
        FormStatus formStatus = FormStatus.Browser;
        readonly string countText = "記錄數：{0}";

        public FormLog(UserModel user, AllDbContext dbContext)
        {
            InitializeComponent();
            try
            {
                this.user = user;
                this.dbContext = dbContext;

                dataGridView1.RowPostPaint += DataGridView1_RowPostPaint;
                dataGridView1.AutoGenerateColumns = false;
                BindData();
                LanguageHelper.SetControlLanguageText(this);
            }
            catch (Exception ex)
            {
                QMessageBox.Show(ex.Message);
                Logger.LogError(ex);
            }
        }

        private void DataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridViewSet.RowPostPaint(sender, e);
        }

        private void tsmEdit_Click(object sender, EventArgs e)
        {

        }

        private void tsmCancel_Click(object sender, EventArgs e)
        {

        }


        private void tsmSave_Click(object sender, EventArgs e)
        {
            try
            {
               
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message,"異常S0002", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        private void tsmDelete_Click(object sender, EventArgs e)
        {
            if(dataGridView1.SelectedRows.Count == 0)
            {
                QMessageBox.Show("請選擇要刪除的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                if (QMessageBox.Show("數據刪除後無法恢復，請確認是否刪除", "請確認", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
    == DialogResult.Cancel)
                    return;
            }


            int i = 0;
            List<LogModel> delList = new List<LogModel>();
            foreach (DataGridViewRow r in dataGridView1.SelectedRows)
            {
                LogModel model = r.DataBoundItem as LogModel;
                if (model != null) delList.Add(model);
                 
            }

            if(delList.Count > 0)
            {
                i = dbContext.QcDb.Deleteable(delList).ExecuteCommand();
            }

            if(i>0)
            {
                QMessageBox.Show("成功刪除記錄 " + i.ToString() + " 條。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                SysLogService.Log(dbContext, user, this, tsmDelete.Text
                    , string.Join(",",delList.Select(t => t.Id).ToArray())
                    , string.Empty);
                BindData();
            }
            else
            {
                QMessageBox.Show("未刪除任何記錄","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void tsmExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {

        }

        void BindData()
        {
            try
            {
                this._Logs = this.dbContext.QcDb.Queryable<LogModel>()
                    .Where(l => l.Time.Date >= dteStartDate.Value.Date)
                    .Where(l => l.Time.Date < dteEndDate.Value.AddDays(1).Date)
                    .Take(3000).ToList();
                dataGridView1.DataSource = new BindingList<LogModel>(_Logs);
                toolStripStatusLabel1.Text = string.Format(countText, this._Logs.Count.ToString());
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message,"異常S0030", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }
}
